# -*- coding: utf-8 -*-
from djapp import models

class Controller:
    def __init__(self): 
        pass
    
    def get_level_dict(self): 
        level_dict = {}
        i = 0
        for lvl in models.Level.objects.all().order_by('level'):
            if lvl.tags:
                tags = [t.strip() for t in lvl.tags.split(',')]
                tags.sort()
                level_dict[i] = {}
                level_dict[i]['name'] = lvl.name
                level_dict[i]['tags'] = tuple(tags)
                level_dict[i]['level'] = lvl.level
                i += 1
        #lvl_dict = {0: {'name': u'первый', 'level': 1, 'tags': [u'tag1.1', u'tag1.2']}, 
        #            1: {'name': u'второй', 'level': 2, 'tags': [u'tag2.1', u'tag2.2', u'tag2.3']},
        #            2: {'name': u'третий', 'level': 3, 'tags': [u'tag3.1', u'tag3.2', u'tag3.3', u"tag3.4"]},
        #            3: {'name': u'четвертый', 'level': 4, 'tags': [u'tag4.1', u'tag4.2', u'tag4.3', u"tag4.4"]}}
        return level_dict
            
            
    def get_user_categories(self):
        category_list = {}; i = 0
        for cat in models.Category.objects.all():
            category_list[i] = {}
            category_list[i]['wave_id'] = cat.wave_id
            category_list[i]['header'] = cat.header
            tags = None
            if cat.tags:
                tags = [t.strip() for t in cat.tags.split(',')]
                tags.sort() 
            category_list[i]['tags'] = tags 
            i += 1  
        #catalog = {0: {'header': u'general', 'wave_id': u'googlewave.com!w+httDVBJBI', 'tags': None}} 
        #           1: {'header': u'test', 'wave_id': u'googlewave.com!w+5gqQlVypB', 'tags': [u'test']}}   
        return category_list #category_list https://wave.google.com/wave/waveref/googlewave.com/w+ 
    
    
    def get_extended_tag_dict(self):
        extended_tag = {}
        for tag in models.ExtendedTag.objects.all():
            try:
                tags = [t.strip().lower() for t in tag.tags.split(',')]
                tags.sort() 
                extended_tag[tag.name] = tags
            except:
                continue  
        return extended_tag